package com.authine.cloudpivot.ext.utils.system;

import com.alibaba.cola.dto.SingleResponse;
import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.ServiceUrlConst;
import com.authine.cloudpivot.ext.utils.HttpClients;
import com.authine.cloudpivot.ext.utils.TypeReferenceUtils;
import com.authine.mvp.app.launcher.dto.bo.response.BO;
import com.authine.mvp.app.launcher.dto.bo.response.BOList;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * @ClassName ReportUtils
 * @Description: 报表导出工具类，方便调用
 * @Author fj
 * @Date 2021/8/26
 * @Version V1.0
 **/
@Slf4j
public class ReportUtils {
    /**
     * 报表导出
     *
     * @param fileName 导出文件名称   示例：inquiry/inquiry_goods_report.jrxml
     * @param fileType 导出文件类型   示例：ReportType.Xls.getId()
     * @param fileData 导出文件数据
     * @return 资源信息
     */
    public static SingleResponse<BO> exportReport(String fileName, String fileType, List<Map<String,Object>> fileData) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.EXPORT_REPORT_URL);
        Map<String, Object> fileParam = Maps.newHashMap();
        fileParam.put("code", fileName);
        fileParam.put("type", fileType);
        fileParam.put("data", fileData);
        String execute = HttpClients.execute(url, fileParam);
        log.info("execute:{}",execute);
        return TypeReferenceUtils.readValueToSingleResponse(execute);
    }

    /**
     * 获取定制报表模板列表
     *
     * @param organizationId    所属组织id
     * @param type              模板类型
     * @return 模板列表
     */
    public static SingleResponse<BOList> getReportTemplateList(String organizationId, String type) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_REPORT_TEMPLATE_LIST_URL);
        Map<String, Object> reportParam = Maps.newHashMap();
        reportParam.put("organizationId", organizationId);
        reportParam.put("type", type);
        String execute = HttpClients.execute(url, reportParam);
        log.info("execute:{}",execute);
        return TypeReferenceUtils.readValueToSingleResponseList(execute);
    }
}
